import {otherRouter,appRouter} from "../../router/routers";
// import {routers} from "./routers"
// import router from './router'
const getChildrenItem=function(item,modulesArr){
  if(!item.meta.hideInMenu){
    // console.log(item.meta.title);
    let childrenArr=[];
    if(item.children&&item.children.length>0){
      item.children.forEach(childrenItem=>{
        var checkItem=getChildrenItem(childrenItem,modulesArr);
        if(checkItem){
          let moduleNo=checkItem.meta.moduleNo;
          if(moduleNo){
            if(!moduleNo || modulesArr.indexOf(moduleNo)>-1){
              childrenArr.push(childrenItem);
            }else{
              //没有对应功能
              console.warn("没有权限::",checkItem)
            }
          }else{
            if(!moduleNo || modulesArr.indexOf(moduleNo)>-1){
              childrenArr.push(childrenItem);
            }
          }

        }
      })
    }
    item.children=childrenArr;
    return item;
  }else {
    return null;
  }
}
export default {
    state: {
      routersData: [otherRouter,...appRouter],//路由列表
      menuList: localStorage.menuList?JSON.parse(localStorage.menuList):[],//菜单列表数据
      modulesArr:localStorage.modulesArr?JSON.parse(localStorage.modulesArr):[],//菜单权限列表
      ControlCenter: null,//
      screenWidth: null,
      screenHeight: null,

    },
    mutations: {
      updateMenuList(state) {
        localStorage.menuList=[];
        let menuList = [];
        // console.log("认证");
        appRouter.forEach(item=>{
          // console.log(item);
          let modulesArr = state.modulesArr;
          //检索子对象
          var itemCheck=getChildrenItem(item,state.modulesArr);
          if(itemCheck){
            // debugger
            let moduleNo=itemCheck.meta.moduleNo;
            if(!moduleNo || modulesArr.indexOf(moduleNo)>-1){
              menuList.push(item);
            }

          }
        })
        state.menuList = menuList;
        // if(menuList.length>0){
          localStorage.menuList = JSON.stringify(menuList);
        // }

        // console.log(state.menuList);
      },
      setModulesArr(state, data) {
        localStorage.modulesArr=[]
        if(data){
          state.modulesArr = data;
          localStorage.modulesArr = JSON.stringify(data);
        }
        // console.log(state.modulesArr);

      },
    },
    actions: {
      //获取权限列表
      getUserModules(context, obj) {
        // debugger
        obj.vm.$app.post('/tmodule/getAllModuleNo')
          .then(res => {
            if (res.data.code == 0) {
              // debugger

              context.commit('setModulesArr', res.data.data);
              context.commit('updateMenuList');
              if (obj.callback) {
                obj.callback();
              }
            }
          })
      },
    }
}
